package com.wr.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.wr.web.dao.UserDao;
import com.wr.web.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户管理")
@RestController
public class UserController {

    @Autowired
    UserDao dao;

    @ApiOperation(value = "首页",notes = "首页")
    @GetMapping("/index1")
    public String login(Model model){
        model.addAttribute("msg",123);
        return "index";
    }

    //分页封装成layui的格式
    @ApiOperation(value = "分页查询",notes = "分页")
    @GetMapping("/userList")
    public JSONObject userList(@RequestParam("page") int page,@RequestParam("limit") int limit){
        //PageRequest.of(当前页,每页大小) 当前页是从0开始
        Pageable pageable = PageRequest.of(page-1,limit);
        Page<User> p = dao.findAll(pageable);
        JSONObject object = new JSONObject();
        object.put("code",0);
        object.put("msg","获取数据成功");
        object.put("data",p.getContent());
        object.put("count",p.getTotalElements());
        return object;
    }
//    接口平台中的信息
    @ApiOperation(value = "用户信息",notes = "所有用户")
    @GetMapping("/userinfo")
    public JSONObject userinfo(){
        List<User> list = dao.findAll();//1
        JSONObject object = new JSONObject();
        object.put("code",0);
        object.put("msg","获取数据成功");
        object.put("data",list);
        return object;
    }

    @GetMapping("/userInfo/{name}")
    public List<User> uerInfo(@PathVariable("name") String name){
        return dao.findByName(name);
    }
}
